package com.example.demo.entity.bean.dataSource1;

import com.example.demo.ApplicationContextProvider;
import com.example.demo.entity.dataSource1.GeographyEntity;
import com.example.demo.entity.dataSource1.StoryContentEntity;
import com.example.demo.entity.dataSource1.StoryEntity;
import com.example.demo.entity.dataSource1.StoryToGeographyEntity;
import com.example.demo.wrap.StoryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.context.ApplicationContext;

import java.util.List;

/**
 * StoryBean包含Story及周边关联数据
 * Created by Wen Gao on 2018/7/17.
 */
@ApiModel(value = "StoryBean对象,包含Story主体和周边关联关系")
public class StoryBean {


    /**
     * Story主键
     * */
    private int storyId;

    /**
     * Story表
     */
    @ApiModelProperty(value = "Story主体,对应story表")
    private StoryEntity storyEntity;
    /**
     * StoryContent表
     */
    @ApiModelProperty(value = "Story内容主体,对应story_content表")
    private StoryContentEntity storyContentEntity;
    /**
     * 对应地区code列表
     */
    @ApiModelProperty(value = "Story对应地区Code列表")
    private List<String> geographyCodeList;
    /**
     * 对应地区关系表
     */
    @ApiModelProperty(value = "Story对应地区GeographyEntity列表,对应story_to_geography表")
    private List<StoryToGeographyEntity> storyToGeographyEntityList;

    /**************************************************************************************************************************************************************************************
     *
     ***************************************************************************************************************************************************************************************/
    public StoryBean() {

    }

    public StoryBean(int storyId) {
        this.setStoryId(storyId);
    }


    /**************************************************************************************************************************************************************************************
     * storyBean的行为
     ***************************************************************************************************************************************************************************************/
    /**
     * 获取一个只包含storyEntity的storyBean,之后可以从storyBean中按需取属性
     */
    public static StoryBean initialize(int storyId) {
        StoryBean storyBean = new StoryBean(storyId);
        storyBean.setStoryEntity(getStoryWrapper().selectStoryEntity(storyId));
        return storyBean;
    }

    /**
     * 获取一个storyBean
     *
     * @param storyId
     */
    public static StoryBean initializeFullStoryBean(int storyId) {
        StoryBean storyBean = new StoryBean();
        storyBean.setStoryEntity(getStoryWrapper().selectStoryEntity(storyId));
        storyBean.setStoryContentEntity(getStoryWrapper().selectStoryContentEntity(storyId));
        // 获取storyToGeography
        StoryToGeographyEntity storyToGeographyEntity = new StoryToGeographyEntity();
        storyToGeographyEntity.initialize();
        storyToGeographyEntity.setStoryId(storyId);
        storyToGeographyEntity.setType(1);
        storyBean.setGeographyCodeList(getStoryWrapper().selectGeographiesByTypeAndStoryId(storyToGeographyEntity));
        return storyBean;
    }


    /**
     * 尝试新增一个完整的storyBean,并返回
     */
    public StoryBean saveStoryBean(StoryBean storyBean) {
        // 保存story本体
        if (storyBean.getStoryEntity() != null) {
            getStoryWrapper().insertStoryEntity(storyBean.getStoryEntity());
            storyId = storyBean.getStoryEntity().getId();// 获取本次插入mysql的自增storyId
        }
        // 保存storyContent
        if (storyBean.getStoryContentEntity() != null) {
            storyBean.getStoryContentEntity().setId(storyId);
            getStoryWrapper().insertStoryContentEntity(storyBean.getStoryContentEntity());
        }
        // 保存storyToGeography
        if (storyBean.getGeographyCodeList() != null) {
            for (String code : storyBean.getGeographyCodeList()) {
                StoryToGeographyEntity storyToGeographyEntity = new StoryToGeographyEntity();
                storyToGeographyEntity.setStoryId(storyId);
                storyToGeographyEntity.setGeographyCode(code);
                storyToGeographyEntity.setType(1);
                getStoryWrapper().insertStoryToGeography(storyToGeographyEntity);
            }
        }
        return storyBean;
    }

    /**
     * 更新完整的storyBean
     *
     * @param storyBean 调用处需要拼装好完整的storyBean
     */
    public StoryBean updateFullStoryBean(StoryBean storyBean) {
        // 如果参数storyEntity和db中的storyEntity不一致,则update storyEntity
        if (storyBean.getStoryEntity() != null && !storyBean.getStoryEntity().equals(storyBean.getStoryEntityDB())) {
            getStoryWrapper().updateStoryEntity(storyBean.getStoryEntity());
        }
        // 如果参数storyContentEntity和db中的storyContentEntity不一致,则update storyContentEntity
        if (storyBean.getStoryContentEntity() != null && !storyBean.getStoryContentEntity().equals(storyBean.getStoryContentEntityDB())) {
            getStoryWrapper().updateStoryContentEntity(storyBean.getStoryContentEntity());
        }
        /**
         * 如果按照delete,然后重新insert逻辑操作storyToGeography
         * */
        getStoryWrapper().deleteGeographiesByStoryId(storyBean.getStoryEntity().getId());
        for (StoryToGeographyEntity storyToGeographyEntity : storyBean.getStoryToGeographyEntityList()) {
            getStoryWrapper().insertStoryToGeography(storyToGeographyEntity);
        }
        /**
         * 如果按照update
         * */
//        // 遍历db中的s2g,如果s2g中的数据在参数中不存在,则删除这条
//        for(StoryToGeographyEntity storyToGeographyEntity : getStoryMapper().selectStoryToGeographyByStoryId()) {// db_s2g
//            for() {
//                if(storyToGeographyEntity.getStoryId() == ) {// 判断该条db中的s2g是否在参数bean的s2g中存在,如果在参数bean中不存在,则删除此条db_s2g
//                    getStoryMapper().deleteGeographiesByStoryIdAndGeoCode(storyToGeographyEntity);
//                }
//            }
//
//        }
//        // 遍历参数中的s2g,如果s2g在db中存在,则update,如果不存在则insert
//        if (storyBean.getStoryToGeographyEntityList() != null) {
//            for (StoryToGeographyEntity storyToGeographyEntity : storyBean.getStoryToGeographyEntityList()) {
//                if (storyToGeographyEntity == null) {
//                    continue;
//                }
//                if (getStoryMapper().selectGeographyByStoryIdAndGeographyCode(storyToGeographyEntity) != null) {
//                    // 如果该条s2g关联关系在db中存在,则update这条
//                    getStoryMapper().updateGeography(storyToGeographyEntity);
//                } else {
//                    // 如果这条s2g关联关系在db中不存在,,则insert这条
//                    getStoryMapper().insertStoryToGeography(storyToGeographyEntity);
//                }
//            }
//        }

        return null;
    }

    public void saveStoryEntity(StoryEntity storyEntity) {
        getStoryWrapper().insertStoryEntity(storyEntity);
    }

    /**
     * 删除整个storyBean
     */
    public void deleteFullStoryBean(int storyId) {
        // TODO 删除storyEntity, storyContentEntity, storyToGeographyList
        // TODO 几乎不会用到.
    }

    /**
     * 只更新storyEntity
     */
    public void updateStoryEntity(StoryEntity storyEntity) {
        getStoryWrapper().updateStoryEntity(storyEntity);
    }

    /**
     * 只更新storyContentEntity
     */
    public void updateStoryContentEntity(StoryContentEntity storyContentEntity) {
        getStoryWrapper().updateStoryContentEntity(storyContentEntity);
    }

    /**
     * 只更新storyToGeography
     */
    public void updateStoryToGeographyList(List<StoryToGeographyEntity> storyToGeographyEntityList) {
        // TODO 更新storyToGeographyList
    }



    /**************************************************************************************************************************************************************************************
     * lazy initial.
     * 定义一部分getXXXDB方法按需从数据库加载数据,freemarker这种模版也可以直接使用
     ***************************************************************************************************************************************************************************************/
    @JsonIgnore
    public StoryEntity getStoryEntityDB() {
        return getStoryWrapper().selectStoryEntity(this.getStoryEntity().getId());
    }

    @JsonIgnore
    public StoryContentEntity getStoryContentEntityDB() {
        return getStoryWrapper().selectStoryContentEntity(this.getStoryEntity().getId());
    }

    @JsonIgnore
    public List<String> getGeographyCodeListDB() {
        StoryToGeographyEntity storyToGeographyEntity = new StoryToGeographyEntity();
        storyToGeographyEntity.initialize();
        storyToGeographyEntity.setStoryId(this.getStoryEntity().getId());
        storyToGeographyEntity.setType(1);
        return getStoryWrapper().selectGeographiesByTypeAndStoryId(storyToGeographyEntity);
    }

    @JsonIgnore
    public List<GeographyEntity> getGeographyEntitiesDB() {
        return getStoryWrapper().selectGeographyListByStoryId(this.getStoryEntity().getId());
    }


    /**************************************************************************************************************************************************************************************
     * getters and setters
     ***************************************************************************************************************************************************************************************/
    public int getStoryId() {
        return storyId;
    }

    public void setStoryId(int storyId) {
        this.storyId = storyId;
    }

    public StoryEntity getStoryEntity() {
        return storyEntity;
    }

    public void setStoryEntity(StoryEntity storyEntity) {
        this.storyEntity = storyEntity;
    }

    public StoryContentEntity getStoryContentEntity() {
        return storyContentEntity;
    }

    public void setStoryContentEntity(StoryContentEntity storyContentEntity) {
        this.storyContentEntity = storyContentEntity;
    }

    public List<String> getGeographyCodeList() {
        return geographyCodeList;
    }

    public void setGeographyCodeList(List<String> geographyCodeList) {
        this.geographyCodeList = geographyCodeList;
    }

    public List<StoryToGeographyEntity> getStoryToGeographyEntityList() {
        return storyToGeographyEntityList;
    }

    public void setStoryToGeographyEntityList(List<StoryToGeographyEntity> storyToGeographyEntityList) {
        this.storyToGeographyEntityList = storyToGeographyEntityList;
    }


    /**
     * 通过ApplicationContext获取Wrapper,不再需要获取各个mapper
     */
    private static StoryWrapper getStoryWrapper() {
        ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
        return applicationContext.getBean(StoryWrapper.class);
    }
}
